package com.zj.weather.bgviews.light;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;

import com.zj.weather.utils.PathAliasChangeUtils;

import java.util.List;

import static com.zj.weather.utils.RandomUtils.getRandom;

/**
 * Created by zhaojie on 2018/8/14.
 */
public class TentacleInfo {

    public final float height;
    public final float minAlpha = 0.02f;
    public float width;
    public final int layer;
    //电触的 parent = 父层级 和 id = 当前的层级
    public final int parent, id;
    public float curX;
    public float curY;
    public Path allPath;
    public Path ownPath;
    public final float speed;
    public float alpha;
    public final float startX, startY;
    public float curXOffset;
    private final float minXOffset = -5, maxXOffset = 5;

    public TentacleInfo(Path curPath, int layer, float curX, float curY, float height, float width, float curOffset, float alpha, float speed, int parent, int id) {
        this.parent = parent;
        this.id = id;
        if (allPath == null) allPath = new Path();
        if (ownPath == null) ownPath = new Path();
        this.height = height;
        this.width = width;
        this.layer = layer;
        this.curX = startX = curX;
        this.curY = startY = curY;
        this.speed = speed;
        if (curPath == null) {
            curPath = new Path();
            curPath.lineTo(0, 0);
            curPath.moveTo(startX, startY);
        }
        this.allPath.set(curPath);
        this.ownPath.moveTo(startX, startY);
        this.alpha = Math.min(1.0f, (alpha / layer) + minAlpha);
        this.curXOffset = curOffset;
    }

    public void resetX(float curXOffset) {
        this.curXOffset = curXOffset;
    }

    public void setNewPosition(float x, float y) {
        this.curX += x + curXOffset + getRandom(minXOffset, maxXOffset);
        this.curY += y;
        this.allPath.lineTo(curX, curY);
        this.ownPath.lineTo(curX, curY);
    }

    public void clear() {
        ownPath.rewind();
        ownPath.reset();
        allPath.rewind();
        allPath.reset();
    }

    public void upDateParams(List<TentacleInfo> tentacles) {
        alpha -= 0.003f;
    }

    public void draw(Paint paint, float maxWidth, float minWidth, boolean isDrawSurplus, float start, float end, float duration, Canvas canvas) {
        PathAliasChangeUtils.PathClipInfo info = PathAliasChangeUtils.getAliasPathClip(start, end, duration, maxWidth, minWidth, false, ownPath);
        if (isDrawSurplus)
            canvas.drawPath(info.surplusPath, paint);
        for (PathAliasChangeUtils.PathChangeInfo changeInfo : info.pathChanges) {
            paint.setStrokeWidth(changeInfo.width);
            canvas.drawPath(changeInfo.path, paint);
        }
    }
}
